package com.zy.design.demo.structural;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合模式
 * 特点：将对象组合成树形结构，使得用户对单个对象和组合对象的使用具有一致性。
 */
interface Component {
    void display();
}

class Leaf implements Component {
    public void display() { System.out.println("叶子节点被显示"); }
}

class Composite implements Component {
    private List<Component> children = new ArrayList<Component>();

    public void add(Component c) { children.add(c); }

    public void display() {
        System.out.println("组合节点被显示");
        for (Component child : children) child.display();
    }
}

class Main2 {
    public static void main(String[] args) {
        Composite root = new Composite();
        root.add(new Leaf());

        Composite sub = new Composite();
        sub.add(new Leaf());
        root.add(sub);

        root.display();
    }
}
